package com.example.business.service;

import cn.hutool.core.bean.BeanUtil;
import com.example.common.response.PageResponse;
import com.example.business.entity.ProductSku;
import com.example.business.entity.ProductSkuExample;
import com.example.business.mapper.ProductSkuMapper;
import com.example.business.request.ProductSkuListRequest;
import com.example.business.response.ProductSkuResponse;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.example.common.util.SnowflakeUtil;
import com.example.business.request.ProductSkuRequest;

@Service
public class ProductSkuService {
    private static final Logger log = LoggerFactory.getLogger(ProductSkuService.class);
    @Autowired
    ProductSkuMapper productSkuMapper;

    public PageResponse<ProductSkuResponse> list(ProductSkuListRequest request) {
        ProductSkuExample productSkuExample = new ProductSkuExample();
        // 按 id 字段逆序排序
        productSkuExample.setOrderByClause("id desc");

        // 查询条件
        ProductSkuExample.Criteria criteria = productSkuExample.createCriteria();

        // 增加查询条件：关键词搜索
        if (request.getKeyword() != null && !request.getKeyword().isEmpty()) {
            String keyword = "%" + request.getKeyword() + "%";
            criteria.andTitleLike(keyword);
        }

        PageHelper.startPage(request.getPage(), request.getSize());
        log.info("当前页码 current_page = {}", request.getPage());
        log.info("每页记录数 per_page = {}", request.getSize());

        List<ProductSku> productSkus = productSkuMapper.selectByExample(productSkuExample);

        PageInfo<ProductSku> pageInfo = new PageInfo<>(productSkus);
        log.info("总记录数 total = {}", pageInfo.getTotal());
        log.info("总页数 pages = {}", pageInfo.getPages());

        List<ProductSkuResponse> list = BeanUtil.copyToList(productSkus, ProductSkuResponse.class);

        PageResponse<ProductSkuResponse> pageResponse = new PageResponse<>();
        pageResponse.setTotal(pageInfo.getTotal());
        pageResponse.setList(list);

        return pageResponse;
    }

    public List<ProductSkuResponse> listByProductId(Long productId) {
        ProductSkuExample example = new ProductSkuExample();
        example.createCriteria().andProductIdEqualTo(productId);
        List<ProductSku> skus = productSkuMapper.selectByExample(example);
        return BeanUtil.copyToList(skus, ProductSkuResponse.class);
    }

    public void save(ProductSkuRequest request) {
        ProductSku productSku = BeanUtil.copyProperties(request, ProductSku.class);
        DateTime now = DateTime.now();
        if (ObjectUtil.isNull(productSku.getId()) || productSku.getId() == 0L) {
            productSku.setId(SnowflakeUtil.getId());
            productSku.setCreatedAt(now);
            productSku.setUpdatedAt(now);
            productSkuMapper.insert(productSku);
        } else {
            productSku.setUpdatedAt(now);
            productSkuMapper.updateByPrimaryKeySelective(productSku);
        }
    }

    public int deleteBatch(List<Long> ids) {
        ProductSkuExample productSkuExample = new ProductSkuExample();
        ProductSkuExample.Criteria criteria = productSkuExample.createCriteria();
        criteria.andIdIn(ids);
        return productSkuMapper.deleteByExample(productSkuExample);
    }

    public ProductSku findById(Long id) {
        return productSkuMapper.selectByPrimaryKey(id);
    }
}
